package cn.cdut.servlet.tools.JDBC;

import java.sql.*;

public class tools_pages {
    private static PreparedStatement preparedStatement=null;
    private static Connection connection=null;
    private static String rname="root";
    private static String rpassword="123456";
    private static String url="jdbc:mysql://localhost:3306/gui?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            connection = DriverManager.getConnection(url, rname,rpassword);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    public static ResultSet find(String sql) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        return  preparedStatement.executeQuery();
    }
    public static void insert(String sql) throws Exception {
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.executeUpdate();
    }
    public static void colse(){
        try {
            preparedStatement.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
